package br.com.fiap.si.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;*/

import br.com.fiap.si.bean.ContaReceber;
import br.com.fiap.si.bean.Usuario;
import br.com.fiap.factory.ConnectionFactory;

public class ContaReceberDAO {

	Connection conn = null;

	public ContaReceberDAO() { //construtor
		try {
			conn = ConnectionFactory.getConnection();
		} catch (SQLException ex) {
		}
	}

	public void insert(ContaReceber cred) {

		String sql = "insert into contareceber(valor, descricao,datainclusao,parcelas,idusuario) "
					+ "values(?,?,STR_TO_DATE(?, '%d/%m/%Y'),?,?)";

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);

			stmt.setString(1, Double.toString(cred.getValor()));
			stmt.setString(2, cred.getDescricao());
			stmt.setString(3, cred.getDatainclusao());
			stmt.setString(4, Integer.toString(cred.getParcelas()));
			stmt.setString(5, Integer.toString(cred.getIdusuario()));

			stmt.executeUpdate();

		} catch (SQLException ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}
	}
	
	public void update(ContaReceber cred, String id) {
		String sql = "UPDATE contareceber SET valor = ?, descricao = ?, datainclusao = STR_TO_DATE(?, '%d/%m/%Y'), parcelas = ?"
					+ " WHERE idcontaReceber = ?";

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);

			stmt.setString(1, Double.toString(cred.getValor()));
			stmt.setString(2, cred.getDescricao());
			stmt.setString(3, cred.getDatainclusao());
			stmt.setString(4, Integer.toString(cred.getParcelas()));
			stmt.setString(5, id);

			stmt.executeUpdate();

		} catch (SQLException ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}
	}
	
	public void delete(String id){
		String sql = "DELETE FROM contareceber WHERE idcontaReceber = ?";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, id);
			stmt.executeUpdate();
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
	}

	public ContaReceber getByID(String idcontaPagar){

		String sql = "SELECT * FROM contareceber WHERE idcontaReceber = ?";
		ContaReceber cred = null;
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, idcontaPagar);
			
			ResultSet rs = stmt.executeQuery();

			if(rs.next()){
				cred = new ContaReceber();
				
				cred.setIdcontaReceber(rs.getInt("idcontaReceber"));
				cred.setValor(rs.getDouble("valor"));
				cred.setDatainclusao(rs.getString("datainclusao"));
				cred.setDescricao(rs.getString("descricao"));
				cred.setParcelas(rs.getInt("parcelas"));
				cred.setIdusuario(rs.getInt("idusuario"));
			}
			
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
		return cred;
		
	}
	
	public List<ContaReceber> getAll(int id) {

		String sql = "SELECT * FROM contareceber where idusuario = ?";
		
		List<ContaReceber> lista = new ArrayList<ContaReceber>();

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, Integer.toString(id));
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				ContaReceber cred = new ContaReceber();
				
				cred.setIdcontaReceber(rs.getInt("idcontaReceber"));
				cred.setValor(rs.getDouble("valor"));
				cred.setDatainclusao(rs.getString("datainclusao"));
				cred.setDescricao(rs.getString("descricao"));
				cred.setParcelas(rs.getInt("parcelas"));
				cred.setIdusuario(rs.getInt("idusuario"));

				lista.add(cred);
			}

		} catch (SQLException ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}

		return lista;
	}

}
